import ast
from classification import load_data_lib
from flask import Flask, request, jsonify

app = Flask(__name__)


class StatusCode:
    SUCCESS_CODE = 200
    FAILURE_CODE = 500


@app.route('/nlp/get_natural_sentence', methods=['POST'])
def get_structured_data():
    try:
        keyword_list = request.form.get('keyword_list', type=str, default=None)
        keyword_list = ast.literal_eval(keyword_list)
        keyword_str = '-'.join(keyword_list)

        keys = data_lib.keys()

        if keyword_str not in keys:
            return jsonify({'status': StatusCode.FAILURE_CODE,
                            'message': 'The key value is not in the current library',
                            'payload': ''})

        payload = []
        for key in keys:
            if key in keyword_str:
                payload.extend(data_lib[key])
        return jsonify({'status': StatusCode.SUCCESS_CODE,
                        'message': '',
                        'payload': str(payload)})
    except:
        return jsonify({'status': StatusCode.FAILURE_CODE,
                        'message': 'something wrong',
                        'payload': ''})


if __name__ == '__main__':
    data_lib = load_data_lib(file_dir='./lib')
    app.run(host='0.0.0.0', port=7575)
